# -*- coding: utf-8 -*-
# HARU-Backend (C++ Version)

from ir.ir_module import IModule

from hbcxx.simcxx_enum import simcxx_convert_enum
from hbcxx.simcxx_pack import simcxx_convert_pack
from hbcxx.simcxx_ports import simcxx_convert_channels
from hbcxx.simcxx_node import simcxx_convert_nodes

from hbcxx.cxx_dumps import *


def simcxx_convert_includes(includes: str):
    include_lines = []
    for include_chain in includes:
        include_lines.append('#include "{}.h"'.format('/'.join(include_chain)))
    return include_lines


def transfer_ir_to_simcxx(ir_module: IModule, filepath: str, source_path: str):
    # Create the header file contents.
    header_file = []
    cpp_file = []
    # Convert the include files.
    header_file += simcxx_convert_includes(ir_module.includes)
    # Convert the module into header and cpp files.
    header_file += simcxx_convert_enum(ir_module.enums)
    header_file += simcxx_convert_pack(ir_module.packs)
    # Now we need to convert all the ports.
    port_header, port_cpp = \
        simcxx_convert_channels(ir_module.channels, ir_module.helper)
    header_file += port_header
    cpp_file += port_cpp
    # The biggest part, convert the nodes.
    node_header, node_cpp = simcxx_convert_nodes(ir_module.nodes)
    header_file += node_header
    cpp_file += node_cpp
    # Get the filename of from source path.
    filename = simcxx_get_filename(source_path)
    # Dump the header and cpp file.
    simcxx_dump_header(filename, filepath, header_file)
    simcxx_dump_cpp(filename, filepath, cpp_file)
